<?php
/**
 * Created by PhpStorm.
 * User: Ren
 * Date: 4/10/16
 * Time: 4:23 PM
 */

namespace app\controllers;


use yii\data\Pagination;
use yii\web\Controller;
use Yii;
use app\models\Uc_user;
use app\models\Pr_support;
use app\models\Rs_raised;

class PrinvestController extends Controller
{
    public function actionShow(){


        if(!empty($_COOKIE['user_id'])){

            $user_id = $_COOKIE['user_id'];
            $user = Uc_user::find()
                ->where([
                    'user_id' => $user_id, //user_id => user_id
                    'del_flag' => 0
                ])
                ->one();

            $view = Yii::$app->view;
            $view->params['title'] = "投资项目";
            PrlikeController::setView($user);

            $query = Pr_support::find()
                ->where([
                    'user_id' => $user_id, //user_id => user_id
                    'del_flag' => 0
                ])->all();
            $inList = "";
            foreach($query as $key=>$id){
                $inList .= $id['raised_id'];
                $inList .= ",";
            }

            if($inList === ""){
                $inList .= -1;
            }else{
                $inList = substr($inList, 0, -1);
            }
            // echo $inList;
            $raised = Rs_raised::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')');

            $pagination = new Pagination([
                'defaultPageSize' => 5,
                'totalCount' => $raised->count()
            ]);
            $myraised = $raised->orderBy('rs_id DESC')
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();

            return $this->render('prinvest.php', ['raised'=>$myraised, 'user_id'=>$user_id, 'pages'=>$pagination]);

        }else{
            echo "<script>alert('您还未登录 请先登录')</script>";
            echo "<script>location.href='http://localhost/chuangwu/web/index.php?r=login/show'</script>";
        }

    }
}